home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / pine3.92 / pine / osdep / readfile.os2 < prev    next >
Text File  |  1996-03-14  |  2KB  |  63 lines

  1. #line 2 "osdep/readfile.os2"
  2. /*----------------------------------------------------------------------
  3.     Read whole file into memory
  4.  
  5.   Args: filename -- path name of file to read
  6.  
  7.   Result: Returns pointer to malloced memory with the contents of the file
  8.           or NULL
  9.  
  10. This won't work very well if the file has NULLs in it and is mostly
  11. intended for fairly small text files.
  12.  
  13. This is a slightly modified version for OS/2 which allows us to do some
  14. sexy things that might not be possible otherwise, such as running a
  15. program to do signatures and so forth. To run a program via a pipe,
  16. just precede the command with a |.
  17.  ----*/
  18.  
  19. char *
  20. read_file(filename)
  21.     char *filename;
  22. {
  23.   int   nb;
  24.   char  *buf =NULL;
  25.  
  26.   if (*filename != '|') {
  27.     int   fd;
  28.     struct stat statbuf;
  29.  
  30.     if ((fd = open(filename, O_RDONLY|O_BINARY)) >= 0) {
  31.       fstat(fd, &statbuf);
  32.       buf = fs_get((size_t)statbuf.st_size + 1);
  33.       if((nb = read(fd, buf, (int)statbuf.st_size)) < 0)
  34.     fs_give((void **)&buf);        /* NULL's buf */
  35.       else
  36.     buf[nb] = '\0';
  37.       close(fd);
  38.     }
  39.   }
  40.   else{
  41.     PIPE_S * sp = open_system_pipe(filename+1, NULL, NULL, PIPE_READ);
  42.     if (sp){
  43.       char *p = tmp_20k_buf;
  44.       char tmp[1024];
  45.  
  46.       while (fgets(tmp, sizeof tmp - 1, sp->ifilep)!=NULL) {
  47.     int len = strlen(tmp);
  48.     if (p - tmp_20k_buf + len > 20480)
  49.       break;
  50.     memcpy(p, tmp, len);
  51.     p += len;
  52.       }
  53.       *p++ = '\0';
  54.       nb = p - tmp_20k_buf;
  55.       close_system_pipe(&sp);
  56.       buf = fs_get(nb);
  57.       memcpy(buf, tmp_20k_buf, nb);
  58.     }
  59.   }
  60.   return buf;
  61. }
  62.  
  63.